/*
 * File Name:               MovePosWithToggle.cs
 * 
 * Description:             继承于MonoBehaviour的类
 * Author:                  yuhaoyu<158521902@qq.com>

 * Create Date:             2017/08/16
 */

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class MovePosWithToggle : MonoBehaviour
{
    public GameObject obj1;
    public GameObject obj2;
    public GameObject obj3;

    Toggle toggle1;
    Toggle toggle2;
    Toggle toggle3;

    Transform trans;

    public float speed = 0.4f;

    private bool isMove = false;
    private float x_target;
    // Use this for 
    private void Awake()
    {
        toggle1 = obj1.GetComponent<Toggle>();
        toggle2 = obj2.GetComponent<Toggle>();
        toggle3 = obj3.GetComponent<Toggle>(); 
    }

    void Start ()
	{
        trans = this.gameObject.transform;
        x_target = trans.localPosition.x;
        toggle1.onValueChanged.AddListener(OnClickObj1);
        toggle2.onValueChanged.AddListener(OnClickObj2);
        toggle3.onValueChanged.AddListener(OnClickObj3);
	}



    // Update is called once per frame
    void Update ()
	{
        if (isMove)
        {
            var x = trans.localPosition.x;
            x = Mathf.Lerp(x, x_target, speed);
            trans.localPosition = new Vector3(x, trans.localPosition.y, trans.localPosition.z);
            if (x == x_target)
                isMove = false;
        }
	}

    void OnClickObj1(bool isClick)
    {
        if (isClick)
        {
            isMove = true;
            x_target = obj1.transform.localPosition.x;
        }
    }

    void OnClickObj3(bool isClick)
    {
        if (isClick)
        {
            isMove = true;
            x_target = obj3.transform.localPosition.x;

        }
    }

    void OnClickObj2(bool isClick)
    {
        if (isClick)
        {
            isMove = true;
            x_target = obj2.transform.localPosition.x;
        }
    }




}
